package com.mobilereports.service;

import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import com.mobilereports.model.Record;
import com.mobilereports.model.Report;
import com.mobilereports.model.ReportColumn;

public class InMemoryReportsService extends ReportsService {

	private List<Report> reports = new LinkedList<Report>();
	private List<ReportColumn> columns = new LinkedList<ReportColumn>();
	
	
	public InMemoryReportsService() {
		Report r1 = new Report();
		r1.setId("r1");
		r1.setName("Spotkania");
		
		Report r2 = new Report();
		r2.setId("r2");
		r2.setName("Baza klientów");
		
		reports.add(r1);
		reports.add(r2);
		
		ReportColumn rc1 = new ReportColumn();
		rc1.setTitle("Region");
		rc1.setName("region");
		rc1.setHierarchyCandidate(true);
		rc1.setMeasure(false);
		
		ReportColumn rc2 = new ReportColumn();
		rc2.setTitle("Zespół");
		rc2.setName("zespol");
		rc2.setHierarchyCandidate(true);
		rc2.setMeasure(false);
		
		ReportColumn rc3 = new ReportColumn();
		rc3.setTitle("Wynik");
		rc3.setName("wynik");
		rc3.setHierarchyCandidate(false);
		rc3.setMeasure(true);
		
		ReportColumn rc4 = new ReportColumn();
		rc4.setTitle("Doradca");
		rc4.setName("doradca");
		rc4.setHierarchyCandidate(true);
		rc4.setMeasure(false);

		ReportColumn rc5 = new ReportColumn();
		rc5.setTitle("Klient");
		rc5.setName("Klient");
		rc5.setHierarchyCandidate(true);
		rc5.setMeasure(false);

		ReportColumn rc6 = new ReportColumn();
		rc6.setTitle("Segment");
		rc6.setName("segment");
		rc6.setHierarchyCandidate(true);
		rc6.setMeasure(false);

		ReportColumn rc7 = new ReportColumn();
		rc7.setTitle("Miesiąc podp umowy");
		rc7.setName("Miesiac");
		rc7.setHierarchyCandidate(true);
		rc7.setMeasure(false);

		ReportColumn rc8 = new ReportColumn();
		rc8.setTitle("Liczba spotkań");
		rc8.setName("liczba_spotkan");
		rc8.setHierarchyCandidate(false);
		rc8.setMeasure(true);
		
		columns.add(rc1);
		columns.add(rc2);
		columns.add(rc3);
		columns.add(rc4);
		columns.add(rc5);
		columns.add(rc6);
		columns.add(rc7);
		columns.add(rc8);
	}
	
	@Override
	public List<Report> getReports() {
		return reports;
	}

	@Override
	public List<ReportColumn> getReportColumns(Report report) {
		return columns;
	}

	@Override
	public List<Record> fetchData(Report report, List<ReportColumn> hierarchy,
			String path) {
		List<Record> records = new LinkedList<Record>();
		
		Random rand = new Random();
		
		for(int i=0;i < rand.nextInt(20) + 6; ++i) {
			Record r = new Record();
			r.setAttribute("path", path + ";" + i);
			for(ReportColumn rc : columns) {
				if(rc.isMeasure()) {
					r.setAttribute(rc.getName(), rand.nextDouble() * 9 + 0.5);
				}
				else {
					r.setAttribute(rc.getName(), rc.getTitle() + "_" + i);
				}
			}
			records.add(r);
		}
		
		return records;
	}

}

